package com.zhaolang.qogir.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhaolang.qogir.R;
import com.zhaolang.qogir.bean.ProcessedOrderInfo;
import com.zhaolang.qogir.view.ExpandableLinearLayout;

import java.util.List;

public class FragmentProcessedNewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private Context mContext;
    private List<ProcessedOrderInfo> orderInfos;
    private ExpandableLinearLayout ell_product;

    public FragmentProcessedNewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<ProcessedOrderInfo> orderInfoList) {
        this.orderInfos = orderInfoList;
    }

    @Override
    public int getCount() {
        return orderInfos != null ? orderInfos.size() : 0;
    }

    @Override
    public ProcessedOrderInfo getItem(int position) {
        return orderInfos != null && position >= 0 & position < orderInfos.size() ? orderInfos
                .get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProcessedOrderInfo itemOrderInfo = getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.fragment_processed_new_middle, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (itemOrderInfo.getOrderTakingNo().equals("0")) {
            holder.tv_processed_order_items.setText("1");
        } else {
            holder.tv_processed_order_items.setText(itemOrderInfo.getOrderTakingNo());
        }

        if (itemOrderInfo.getUserName().equals("")) {
            holder.tv_processed_name.setText(mContext.getString(R.string.processed_unkonw_name));
        } else {
            holder.tv_processed_name.setText(itemOrderInfo.getUserName());
        }

        if (itemOrderInfo.getUserAddress().equals("")) {
            holder.tv_processed_order_address.setText(R.string.processed_unkonw_address);
        } else {
            holder.tv_processed_order_address.setText(itemOrderInfo.getUserAddress());
        }
        holder.tv_processed_order_status.setText("已接单");
        holder.tv_processed_comment.setText("用户评价");
        holder.tv_processed_distance.setText("[0.1公里]");
        holder.tv_processed_order_time.setText(itemOrderInfo.getPayTime());
        holder.tv_processed_delivery_status.setText("配送中");
        holder.tv_processed_product.setText(itemOrderInfo.getOrderProductName());
        holder.tv_processed_product_num.setText("x 1");
        holder.tv_processed_product_price.setText("16.00");
        holder.tv_processed_pay.setText(itemOrderInfo.getPay());
        holder.tv_processed_revenue.setText(itemOrderInfo.getPay());
        holder.tv_processed_order_no.setText(itemOrderInfo.getOrderNo());
        holder.tv_processed_order_taking_time.setText(itemOrderInfo.getOrderTime());
        holder.tv_processed_closed_time.setText(itemOrderInfo.getPayTime());
        holder.tv_processed_return.setText("部分退款");
        holder.tv_processed_print.setText("打印订单");
        holder.ell_product.setOnStateChangeListener(new ExpandableLinearLayout.OnStateChangeListener() {
            @Override
            public void onStateChanged(boolean isExpanded) {
                doArrowAnim(isExpanded, holder);        //根据状态箭头旋转
                //根据状态更改文字提示
                if (isExpanded) {
                    //展开
                    holder.tv_paid_status.setBackground(mContext.getResources().getDrawable(R.drawable.processed_button_retract));
                } else {
                    holder.tv_paid_status.setBackground(mContext.getResources().getDrawable(R.drawable.processed_button_spread));
                }
            }
        });

        holder.tv_paid_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.ell_product.toggle();
            }
        });

        holder.ell_product.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });

        return convertView;
    }

    // 箭头的动画
    private void doArrowAnim(boolean isExpand, ViewHolder holder) {
        if (isExpand) {
            // 当前是展开，箭头由下变为上
            holder.ui_layout_expand.setVisibility(View.VISIBLE);
        } else {
            // 当前是收起，箭头由上变为下
            holder.ui_layout_expand.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_processed_order_items;       //新订单页面序号
        private TextView tv_processed_order_status;      //订单状态（已接单 / 未接单）
        private TextView tv_processed_name;              //订单名字
        private TextView tv_processed_comment;           //用户上次评价
        private TextView tv_processed_order_address;      //用户地址
        private TextView tv_processed_distance;          //距离店家地址
        private TextView tv_processed_order_time;        //用户下单时间
        private TextView tv_processed_delivery_status;   //订单配送状态
        private TextView tv_processed_product;           //用户订购产品名称
        private TextView tv_processed_product_num;       //用户订购产品数量
        private TextView tv_processed_product_price;     //产品价格
        private TextView tv_processed_pay;               //用户支付价格
        private TextView tv_processed_revenue;           //本单商家收入
        private TextView tv_processed_order_no;          //订单编号
        private TextView tv_processed_order_taking_time; //用户下单时间
        private TextView tv_processed_closed_time;       //订单完成时间
        private TextView tv_processed_return;            //部分退款
        private TextView tv_processed_print;             //订单打印
        private ExpandableLinearLayout ell_product;      //扩展按钮
        private TextView tv_paid_status;                 //支付按钮
        private LinearLayout ui_layout_expand;           //扩展部分
        private TextView tv_processed_paid;              //支付状态（已经支付 / 未支付）

        public ViewHolder(View itemView) {
            super(itemView);
            tv_processed_order_items = itemView.findViewById(R.id.tv_processed_order_items);
            tv_processed_order_status = itemView.findViewById(R.id.tv_processed_order_status);
            tv_processed_name = itemView.findViewById(R.id.tv_processed_name);
            tv_processed_comment = itemView.findViewById(R.id.tv_processed_comment);
            tv_processed_order_address = itemView.findViewById(R.id.tv_processed_order_address);
            tv_processed_distance = itemView.findViewById(R.id.tv_processed_distance);
            tv_processed_order_time = itemView.findViewById(R.id.tv_processed_order_time);
            tv_processed_delivery_status = itemView.findViewById(R.id.tv_processed_delivery_status);
            tv_processed_product = itemView.findViewById(R.id.tv_processed_product);
            tv_processed_product_num = itemView.findViewById(R.id.tv_processed_product_num);
            tv_processed_product_price = itemView.findViewById(R.id.tv_processed_product_price);
            tv_processed_revenue = itemView.findViewById(R.id.tv_processed_revenue);
            tv_processed_order_no = itemView.findViewById(R.id.tv_processed_order_no);
            tv_processed_closed_time = itemView.findViewById(R.id.tv_processed_closed_time);
            tv_processed_print = itemView.findViewById(R.id.tv_processed_print);
            tv_processed_pay = itemView.findViewById(R.id.tv_processed_pay);
            tv_processed_order_taking_time = itemView.findViewById(R.id.tv_processed_order_taking_time);
            tv_processed_return = itemView.findViewById(R.id.tv_processed_return);
            ell_product = (ExpandableLinearLayout) itemView.findViewById(R.id.ell_product);
            tv_paid_status = itemView.findViewById(R.id.tv_paid_status);
            ui_layout_expand = itemView.findViewById(R.id.ui_layout_expand);
            tv_processed_paid =  itemView.findViewById(R.id.tv_processed_paid);
        }
    }
}
